//
//  SettingViewController.m
//  SelectionForYou
//
//  Created by qianfeng on 15-6-12.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "SettingViewController.h"
#import "RootViewController.h"

#import "EmailViewController.h"

#import "SDImageCache.h"
@interface SettingViewController ()

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:nil];
    
    self.navigationItem.leftBarButtonItem=item2;
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)homeButton:(UIButton *)sender {
    NSLog(@"%ld",sender.tag);
   // RootViewController *root=[[RootViewController alloc] init];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (IBAction)tellUsButton:(UIButton *)sender {
     NSLog(@"%ld",sender.tag);
    EmailViewController *email=[[EmailViewController alloc] init];
    [self.navigationController pushViewController:email animated:YES];
   // [self presentViewController:email animated:YES completion:nil];
    
}

- (IBAction)clearButton:(UIButton *)sender {
    
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"清除缓存" message:[NSString stringWithFormat:@"总共有%.2fM缓存",[self getCachSize]] preferredStyle:UIAlertControllerStyleActionSheet];
    //增加按钮
    [sheet addAction:[UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        //删除
        //删除两部分
        //1.删除 sd 图片缓存
        //先清除内存中的图片缓存
        [[SDImageCache sharedImageCache] clearMemory] ;
        //清除磁盘的缓存
        [[SDImageCache sharedImageCache] clearDisk];
        
        //2.删除自己缓存
        NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
        [[NSFileManager defaultManager] removeItemAtPath:myCachePath error:nil];
        
        
        
    }]];
    [sheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        //取消
    }]];
    //跳转
    [self presentViewController:sheet animated:YES completion:nil];
    

    
}
- (CGFloat)getCachSize {
       NSUInteger imageCacheSize = [[SDImageCache sharedImageCache] getSize];
        
    NSString *myCachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/MyCaches"];
    NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:myCachePath];
    __block NSUInteger count = 0;
    //2.遍历
    for (NSString *fileName in enumerator) {
        NSString *path = [myCachePath stringByAppendingPathComponent:fileName];
        NSDictionary *fileDict = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
        count += fileDict.fileSize;//自定义所有缓存大小
    }
    // 得到是字节  转化为M
    CGFloat totalSize = ((CGFloat)imageCacheSize+count)/1024/1024;
    return totalSize;
}

@end
